#!/bin/sh
#
# Copyright (C) 2000-2020 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory using the compressed option
# then restore it.
# XPARAM ZSTD_LEVEL=(ZSTD|10:ZSTD1|10:ZSTD3|10:ZSTD10|10:ZSTD19|11:NONE)

TestName="zstd-test"
JobName=zstd
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
#echo "${cwd}/build/po/fr.po" >${cwd}/tmp/file-list
echo "${cwd}/build" >${cwd}/tmp/file-list

if ! grep ZSTD $cwd/build/config.out | grep yes > /dev/null ; then
    print_debug "ERROR: ZSTD not available here"
    exit 1
fi

start_test

if [ "$ZSTD_LEVEL" = "NONE" ] ; then
   sed -i "s/compression\s*=\s*ZSTD\s*//" $conf/bacula-dir.conf
elif [ -n "$ZSTD_LEVEL" ] ; then
   sed -i "s/compression\s*=\s*ZSTD\s*/compression=${ZSTD_LEVEL}/" $conf/bacula-dir.conf
else
   ZSTD_LEVEL="ZSTD"
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug level=1 client
status all
messages
label storage=File volume=TestVolume001
setdebug level=0 client
run job=ZSTDTest storage=File yes
wait
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bacula-restores fileset=ZSTDSet select storage=File
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
stop_bacula


mkdir -p $tmp/bextract
$bin/bextract -V TestVolume001 FileStorage $tmp/bextract
$rscripts/diff.pl -s $cwd/build -d $tmp/bextract/$cwd/build
if [ $? != 0 ] ; then
   print_debug "ERROR: bextract issue"
   bstat=1
fi

check_two_logs
check_restore_diff
grep " Software Compression" ${cwd}/tmp/log1.out | grep "%" 2>&1 1>/dev/null
if [ $? != 0 ] ; then
   echo "  !!!!! No compression !!!!!"
   bstat=1
fi

if [ -n "$REGRESS_DEBUG" ] ; then
   # Show ZSTD time and size
   T=`grep "Elapsed time:" ${cwd}/tmp/log1.out`
   SZ=`grep "FD Bytes Written:" ${cwd}/tmp/log1.out`
   echo "COMPRESSION $ZSTD_LEVEL --> $SZ $T"
fi

end_test
